﻿
var ns, toast, actionsheet;
var app = new Vue({
    el: '#app',
    data: {
        userinfo: {
            headimg: '../image/default.png',
            username: '',
            phonenumber: '',
            loginname: '',
            sex:'',
            positionname:'',
        }
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getuserinfo',
            }, function(succeed, data, err) {
                if (succeed) {
                    that.userinfo = data;
                    api.getPrefs({
                        key: 'headimg'
                    }, function(ret, err) {
                        if (ret.value && ret.value != '') {
                            that.image_cache(ret.value);
                        }
                    });
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
            // api.getPrefs({ key: 'uid' }, function(ret, err) {
            //     if (ret.value) {
            //         api.getPrefs({ key: 'username' }, function(ret, err) {
            //             if (ret.value && ret.value != '') {
            //                 that.userinfo.username = ret.value;
            //             }
            //         });
            //         api.getPrefs({ key: 'headimg' }, function(ret, err) {
            //             if (ret.value && ret.value != '') {
            //                 that.image_cache(ret.value);
            //             }
            //         });
            //         api.getPrefs({ key: 'phonenumber' }, function(ret, err) {
            //             if (ret.value && ret.value != '') {
            //                 that.userinfo.phonenumber = ret.value;
            //             }
            //         });
            //         api.getPrefs({ key: 'loginname' }, function(ret, err) {
            //             if (ret.value && ret.value != '') {
            //                 that.userinfo.loginname = ret.value;
            //             }
            //         });
            //     } else {
            //         that.logout();
            //     }
            // });
        },
        change_nickname: function() {
            var that = this;
            ns.openWin('xiugainicheng_frm', '修改昵称');
        },
        change_phone: function() {
            var that = this;
            ns.openWin('xiugaidianhua_frm', '修改手机号码');
        },
        openActionsheet: function() {
            var that = this;
            actionsheet.init({
                frameBounces: true, //当前页面是否弹动，（主要针对安卓端）
                cancelTitle: '关闭',
                buttons: ['拍照', '图库选择']
            }, function(ret) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        that.take_picture();
                    } else if (ret.buttonIndex == 2) {
                        that.choose_picture();
                    }
                }
            })
        },
        take_picture: function() {
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: true,
                quality: 80,
                saveToPhotoAlbum: false
            }, function(ret, err) {
                if (ret && ret.data) {
                    that.userinfo.headimg = ret.data;
                    that.do_save_headimg();
                }
            });
        },
        choose_picture: function() {
            var that = this;
            api.getPicture({
                sourceType: 'album',
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: true,
                quality: 80,
            }, function(ret, err) {
                if (ret && ret.data) {
                    that.userinfo.headimg = ret.data;
                    that.do_save_headimg();
                }
            });
        },
        do_save_headimg: function() {
            var that = this;
            var filelist = [];
            filelist.push(that.userinfo.headimg);
            toast.loading({
                title: "提交中",
                duration: 2000
            }, function(ret) {});
            ns.post({
                action: 'saveheadimg'
            }, function(succeed, data, err) {
                toast.hide();
                that.is_searching = false;
                if (succeed) {
                    if (data.headimg) {
                        api.setPrefs({
                            key: 'headimg',
                            value: data.headimg
                        });
                        // that.image_cache(data.headimg);
                        api.sendEvent({
                            name: 'changecentersuccess'
                        });
                        api.toast({
                            msg: '头像上传成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                }
            });
        },
        image_cache: function(url) {
            var that = this;
            api.imageCache({
                url: url
            }, function(ret, err) {
                if (ret.status) {
                    that.userinfo.headimg = ret.url;
                }
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    app.get_data();
    api.addEventListener({
        name: 'changecentersuccess'
    }, function() {
        app.get_data();
    });
}
